//
//  UINavigationController+CustomPush.m
//  YJJ0
//
//  Created by 王—wagn on 16/7/11.
//  Copyright © 2016年 王—wagn. All rights reserved.
//

#import "UINavigationController+CustomPush.h"
#import "BaseViewController.h"
#import <objc/runtime.h>

@implementation UINavigationController (CustomPush)
-(void)pushYJJViewController:(BaseViewController *)viewController animated:(BOOL)animated{
    if ([self.topViewController isKindOfClass:[BaseViewController class]]) {
        BaseViewController *topVC = (BaseViewController *)self.topViewController;
        if (topVC.isAnimation) {
            return;
        }else{
            viewController.isAnimation = YES;
            viewController.hidesBottomBarWhenPushed = YES;
            [viewController setYJJLeftButton];
            [self pushYJJViewController:viewController animated:YES];
        }
    } else {
        [self pushYJJViewController:viewController animated:animated];
     }
}


- (void)pushViewController:(BaseViewController *)viewController {
    [self pushYJJViewController:viewController animated:YES];
}

-(NSArray *)yjjPopToViewController:(BaseViewController *)viewController animated:(BOOL)animated{
    if ([self.topViewController isKindOfClass:[BaseViewController class]]) {
        BaseViewController *topVC = (BaseViewController *)self.topViewController;
        if (!topVC.isAnimation) {
            return [self yjjPopToViewController:viewController animated:animated];
        } else {
            return nil;
        }
    } else {
        return [self yjjPopToViewController:viewController animated:animated];
    }
}

- (NSArray *)yjjPopToRootViewControllerAnimated:(BOOL)animated {
    if ([self.topViewController isKindOfClass:[BaseViewController class]]) {
        BaseViewController *topVC = (BaseViewController *)self.topViewController;
        if (!topVC.isAnimation) {
            return [self yjjPopToRootViewControllerAnimated:animated];
        } else {
            return nil;
        }
    } else {
        return [self yjjPopToRootViewControllerAnimated:animated];
    }
}

- (UIViewController *)yjjPopViewControllerAnimated:(BOOL)animated {
    if ([self.topViewController isKindOfClass:[BaseViewController class]]) {
        BaseViewController *topVC = (BaseViewController *)self.topViewController;
        if (!topVC.isAnimation) {
            return [self yjjPopViewControllerAnimated:animated];
        } else {
            return nil;
        }
    } else {
        return [self yjjPopViewControllerAnimated:animated];
    }
}

+ (void)load {
//    -- Exchange the original implementation with our custom one.
    
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(popToViewController:animated:)), class_getInstanceMethod(self, @selector(yjjPopToViewController:animated:)));
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(popToRootViewControllerAnimated:)), class_getInstanceMethod(self, @selector(yjjPopToRootViewControllerAnimated:)));
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(popViewControllerAnimated:)), class_getInstanceMethod(self, @selector(yjjPopViewControllerAnimated:)));
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(popToViewController:animated:)), class_getInstanceMethod(self, @selector(yjjPopToViewController:animated:)));
    

}

@end
